<template>
  <div>
    <el-tree :data="menus" :props="defaultProps" @node-click="handleNodeClick" :expand-on-click-node="false" show-checkbox node-key="catId"
             :default-expanded-keys="expandedKey"
             :draggable="true"
    >
    <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level <= 2"
            type="text"
            size="mini"
            @click="() => append(data)">
            Append
          </el-button>
          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)">
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      >
      <el-form :model="category">
        <el-form-item label="活动名称">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitData">确 定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
// import引入的组件需要注入到对象中才能使用
    data () {
      return {
        category: {name: '', parentCid: 0, catLevel: 0, showStatus: 1, sort: 0},
        menus: [],
        expandedKey: [],
        dialogVisible: false,
        defaultProps: {
          children: 'children',
          label: 'name'
        }
      }
    },
    methods: {
      handleNodeClick (data) {
        console.log(data)
      },
      getMenus () {
        this.$http({
          url: this.$http.adornUrl('/product/category/list/tree'),
          method: 'get'
        }).then(({data}) => {
          console.log('成功获取到数据...', data.data)
          this.menus = data.data
        })
      },
      append (data) {
        this.dialogVisible = true
        console.log('append', data)
        this.category.parentCid = data.catId
        this.category.catLevel = data.catLevel * 1 + 1
      },
      remove (node, data) {
        this.$confirm(`是否删除【${data.name}】菜单?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var ids = [data.catId]
          this.$http({
            url: this.$http.adornUrl('/product/category/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log('删除成功')
              this.$message({
                message: '菜单删除成功',
                type: 'success'
              })
              // 刷新出新的菜单
              this.getMenus()
              // 设置要展开的节点
              this.expandedKey = [node.parent.data.catId]
            } else {
              this.$message.error(data.msg)
            }
          })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })

        console.log('remove', node, data)
      },
      submitData () {
        console.log('提交的分类数据', this.category)
      }
    },

// 监听属性 类似于data概念
    computed: {},
// 监控data中的数据变化
    watch: {},
// 生命周期 - 创建完成（可以访问当前this实例
    created () {
      this.getMenus()
    },
    beforeCreate () {
    }, // 生命周期 - 创建之前",
    beforeMount () {
    }, // 生命周期 - 挂载之前",
    beforeUpdate () {
    }, // 生命周期 - 更新之前",
    updated () {
    }, // 生命周期 - 更新之后",
    beforeDestroy () {
    }, // 生命周期 - 销毁之前",
    destroyed () {
    }, // 生命周期 - 销毁完成",
    activated () {
    } // 如果页面有keep-alive缓存功能，这个函数会触发",
  }
</script>

<style scoped>

</style>
